package com.gitee.smsdistribution.repository;

import com.gitee.smsdistribution.model.SmsInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public interface SmsInfoRepository extends JpaRepository<SmsInfo, Long>, JpaSpecificationExecutor<SmsInfo> {

//    @Query("SELECT s FROM SmsInfo s WHERE (s.company LIKE %:keyword% OR s.contact LIKE %:keyword% OR s.phone LIKE %:keyword% OR s.templateName LIKE %:keyword%) AND s.sendDate BETWEEN :startDate AND :endDate")

    // 分页查询方法，根据keyword模糊查询所有字段，并根据可选的日期范围过滤
    @Query(value = "SELECT s FROM SmsInfo s WHERE " +
        "(s.company LIKE %:keyword% OR s.contact LIKE %:keyword% OR s.phone LIKE %:keyword% OR s.templateName LIKE %:keyword%) " +
        "AND (:startDate IS NULL OR s.sendDate >= :startDate) " +
        "AND (:endDate IS NULL OR s.sendDate <= :endDate)")
    Page<SmsInfo> searchByKeyword(@Param("keyword") String keyword, @Param("startDate") Date startDate,
                                  @Param("endDate") Date endDate, Pageable pageable);
}
